
class Layouts < Controller

  def index e
    layouts = PageThing.all.map { |page|
      title = page.title
      if title.nil? || title.empty?
        title = page.identity
      end
      Element.new(:a, title, :href => e.sublink('show/'+page.id.to_s))
    }.map { |i| ("<li>"+i.to_s+"</li>") }.join

    "<dev class=\"layouts\">"+
      "<h1>Layouty</h1><ul class=\"layout_list\" >"+layouts+"</ul>"+
      Element.new(
        :a,
        "+ přidej",
        :href => e.sublink('add'),
        :class => :button
      ).to_s+
    "</div>"
  end

  def show e
    layout = PageThing.get(e.env[:unprocessed_tail].last.to_i)

    form = layout.creator_form(e)

    if layout.destroyed?
      e.redirect e.link('index')
    end

    <<HTML
<h1>Layout</h1>
      #{form}
HTML
  end

  def add e
    layout = PageThing.new
    if e.request.post?
      layout.default_template
      layout.recieve e
      e.redirect e.link('show/'+layout.id.to_s)
    end
    layout.default_template

    Element.new(:h1, "Nový layout").to_s +
    layout.creator_form(e).to_s
  end
end
